package com.jingyanku.struts2;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.base.Pagination.Pagination;
import com.jingyanku.pojo.Board;
import com.jingyanku.pojo.Category;
import com.jingyanku.pojo.Thread;
import com.jingyanku.service.IBoardService;
import com.jingyanku.service.ICategoryService;
import com.jingyanku.service.IThreadService;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Person;
import com.system.service.IPersonService;

public class BoardAction extends ActionSupport {
	private Board board;
	private int[] adminId;
	private Person person;
	private Thread thread;
	private Category category;
	private IBoardService<Board> boardSer;
	private IPersonService<Person> personSer;
	private IThreadService<Thread> threadSer;
	private ICategoryService<Category> categorySer;
	public List<Category> cateList = new ArrayList<Category>();
	public List<Person> personList = new ArrayList<Person>();
	public List<Thread> threadList = new ArrayList<Thread>();
	public List<Thread> hotList = new ArrayList<Thread>();
	// public List<Board> boardList = new ArrayList<Board>();;
	private Pagination pagination;
	HttpServletRequest request = null;
	HttpServletResponse response = null;// 
	private boolean flag = false;
	private String result;

	public String list() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();

		if (result!=null&&result.equals("hotThread")) {
			// 排名前20的帖子
			hotList = threadSer.list(" from Thread as t where t.deleted = false order by t.hit desc  ",0, 20, null);
			return "hotThread";
		} else {
			board = boardSer.find(Board.class, board.getId());

			int totalCount = threadSer.getTotalCount(
					" select count(t) from Thread t "
							+ " where t.deleted = false and t.board.id = "
							+ board.getId(), null);

			pagination = new Pagination(request, response);
			pagination.setRecordCount(totalCount);

			threadList = threadSer.list(
					"from Thread t  where t.deleted = false and t.board.id = "
							+ board.getId(), pagination.getFirstResult(),
					pagination.getPageSize(), null);

			return "list";
		}
	}

	public String initAdd() {
		cateList = categorySer
				.list("from Category as c where c.deleted = false");
		return "initAdd";
	}

	public String add() {
		request = ServletActionContext.getRequest();
		category = categorySer.find(Category.class, category.getId());

		board.setCategory(category);
		board.setDateCreated(new Date());
		board.setDeleted(false);
		board.setReplyCount(0);
		board.setThreadCount(0);
		board.setVersion("1");

		flag = boardSer.create(board);
		if (flag = false) {
			addActionMessage("类别 " + board.getName() + " 已经存在。");
		}
		request.setAttribute("op", "add");
		return "success";
	}

	public String load() {
		board = boardSer.find(Board.class, board.getId());
		cateList = categorySer
				.list("from Category as c where c.deleted = false");
		return "load";
	}

	public String modify() {
		Board boardFd = boardSer.find(Board.class, board.getId());
		boardFd.setName(board.getName());
		boardFd.setDescription(board.getDescription());
		boardSer.save(boardFd);
		return "success";
	}

	public String delete() {
		board = boardSer.find(Board.class, board.getId());
		board.setDeleted(true);
		boardSer.save(board);
		return "success";
	}

	public String initSetAdmin() {

		board = boardSer.find(Board.class, board.getId());

		personList = personSer.list(" from Person p where p.deleted = false ");

		int[] adminId = new int[board.getAdministrators().size()];

		int i = 0;
		for (Iterator<Person> it = board.getAdministrators().iterator(); it
				.hasNext(); i++) {
			Person p = it.next();
			adminId[i] = p.getId();
		}
		this.setAdminId(adminId);
		return "initSetAdmin";
	}

	public String setAdmin() {
		request = ServletActionContext.getRequest();

		board = boardSer.find(Board.class, board.getId());

		board.getAdministrators().clear();

		int[] adminId = this.getAdminId();

		for (int i = 0; adminId != null && i < adminId.length; i++) {
			Person person = personSer.find(Person.class, adminId[i]);
			board.getAdministrators().add(person);
		}

		boardSer.save(board);
		request.setAttribute("op", "setAdmin");

		return "success";
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public int[] getAdminId() {
		return adminId;
	}

	public void setAdminId(int[] adminId) {
		this.adminId = adminId;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Thread getThread() {
		return thread;
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public IBoardService<Board> getBoardSer() {
		return boardSer;
	}

	public void setBoardSer(IBoardService<Board> boardSer) {
		this.boardSer = boardSer;
	}

	public IPersonService<Person> getPersonSer() {
		return personSer;
	}

	public void setPersonSer(IPersonService<Person> personSer) {
		this.personSer = personSer;
	}

	public IThreadService<Thread> getThreadSer() {
		return threadSer;
	}

	public void setThreadSer(IThreadService<Thread> threadSer) {
		this.threadSer = threadSer;
	}

	public ICategoryService<Category> getCategorySer() {
		return categorySer;
	}

	public void setCategorySer(ICategoryService<Category> categorySer) {
		this.categorySer = categorySer;
	}

	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	public List<Person> getPersonList() {
		return personList;
	}

	public void setPersonList(List<Person> personList) {
		this.personList = personList;
	}

	public List<Thread> getThreadList() {
		return threadList;
	}

	public void setThreadList(List<Thread> threadList) {
		this.threadList = threadList;
	}

	public List<Thread> getHotList() {
		return hotList;
	}

	public void setHotList(List<Thread> hotList) {
		this.hotList = hotList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	// public List<Board> getBoardList() {
	// return boardList;
	// }
	//
	// public void setBoardList(List<Board> boardList) {
	// this.boardList = boardList;
	// }

}
